/* ****************************************************************************
stablepool.h
-------------------------------------------------------------------------------

Copyright (c) 2017, Tain L.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************** */

#if !defined(SPEARS_SPEARS_STABLEPOOL_H)
#define SPEARS_SPEARS_STABLEPOOL_H

#include "../my-prerequisites.h"
#include "pool.h"

namespace Spears
{
	template<typename _ItemType>
	class StablePool : public Pool<_ItemType>
	{
	private: // constructors & deconstructors
		StablePool()
			: m_count(0)
		{};

		StablePool(const StablePool& rhs) = delete;
		StablePool(StablePool&& rhs) = delete;
		virtual ~StablePool()
		{
			for (auto& item : m_items)
			{
				safe_delete(item);
			}
		};

		StablePool& operator=(const StablePool& rhs) = delete;
		StablePool& operator=(StablePool&& rhs) = delete;

	public: // static methods
		static StablePool* get_instance_ptr()
		{
			if (nullptr == m_the_instance)
			{
				std::unique_lock<std::mutex>(m_the_mtx);
				if (nullptr == m_the_instance)
					m_the_instance = new StablePool<_ItemType>();
			}

			return m_the_instance;
		}

		static StablePool& get_instance()
		{
			if (nullptr == m_the_instance)
			{
				std::unique_lock<std::mutex>(m_the_mtx);
				if (nullptr == m_the_instance)
					m_the_instance = new StablePool<_ItemType>();
			}

			return *m_the_instance;
		}

		static void release()
		{
			std::unique_lock<std::mutex>(m_the_mtx);
			safe_delete(m_the_instance);
		}


	public: // methods
		virtual Addr manage_item(_ItemType*& p_item) override
		{
			assert(nullptr != p_item);

			m_items.push_back(p_item);
			p_item = nullptr;

			return Addr(this, m_count++);
		}

		virtual Addr manage_item(_ItemType*& p_item, const std::string& name) override
		{
			Addr res = manage_item(p_item);
			m_name_map[name] = res.index;
			return res;
		}

		virtual _ItemType& item(index idx) override
		{
			assert(idx < m_count && INVALID_INDEX != idx);

			return (*m_items[idx]);
		}

		virtual _ItemType& item(const std::string& name) override
		{
			BEGIN_TRY();
			return (*m_items[m_name_map[name]]);
			CATCH_AND_THROW_ALL();
		}

		virtual _ItemType& operator[](index idx) override
		{
			return item(idx);
		}

		virtual _ItemType& operator[](const std::string& name) override
		{
			return item(name);
		}

		virtual std::size_t	count() const override
		{
			return m_items.size();
		}

	private:
		index									m_count;
		std::vector<_ItemType*>					m_items;
		std::unordered_map<std::string, index>	m_name_map;
		
	private:
		static StablePool*	m_the_instance;
		static std::mutex	m_the_mtx;
	};

	template<typename _ItemType>
	StablePool<_ItemType>* StablePool<_ItemType>::m_the_instance = nullptr;

	template<typename _ItemType>
	std::mutex StablePool<_ItemType>::m_the_mtx;
}

#endif // !SPEARS_SPEARS_STABLEPOOL_H